﻿/*
    Copyright (C) 2008, wj2929, wj2929@gmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    *KEEP THIS HEADER INFORMATION*
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using mjRegister.BAL;
using mjRegister.Entity;
using System.Text.RegularExpressions;

namespace mjRegister
{
    public partial class DisplaySendCar : Form
    {
        CookieCollection cc = new CookieCollection();
        public VerifyCode verifyCode = new VerifyCode();
        public string code = string.Empty;

        private IList<CarInfo> _carInfos = new List<CarInfo>();
        private SimpleUserInfo _giveCarUserInfo;

        private CarInfo _giveCarInfo;

        public string carid = string.Empty;

        public DisplaySendCar(IList<CarInfo> carInfos,SimpleUserInfo giveCarUserInfo)
        {
            _carInfos = carInfos;
            _giveCarUserInfo = giveCarUserInfo;

            InitializeComponent();
        }

        public DisplaySendCar(IList<CarInfo> carInfos,CarInfo giveCarInfo, SimpleUserInfo giveCarUserInfo)
        {
            _carInfos = carInfos;

            _giveCarInfo = giveCarInfo;

            _giveCarUserInfo = giveCarUserInfo;

            InitializeComponent();
        }

        public void GetExt()
        {
            this.piccode.Image = verifyCode.GetVerifyCodeImage();
        }

        private void DisplaySendCarVerifyCode_Load(object sender, EventArgs e)
        {
            this.label1.Text = string.Format("送车给：[{0},{1}]", _giveCarUserInfo.uid, _giveCarUserInfo.real_name);

            string re = ".*?-(?<color>\\d+).gif";

            for (int i = 0; i < _carInfos.Count; i++)
            {
                CarInfo carInfo = _carInfos[i];

                string color = "未获取";

                try
                {
                    if (Regex.IsMatch(carInfo.car_logo_big, re))
                    {
                        color = Regex.Match(carInfo.car_logo_big, re).Result("${color}");
                        color = ((CarColor)int.Parse(color)).ToString();
                    }
                }
                catch
                {
                    color = "未获取";
                }
                ListViewItem listViewItem = new ListViewItem(
                    new string[]{
                        carInfo.carid.ToString(),
                        string.Format("{0}，{1}，{2}",carInfo.car_name,color,carInfo.park_profit.ToString()),
                        carInfo.price.ToString()
                    });

                if (_giveCarInfo != null && _giveCarInfo.carid == carInfo.carid)
                    listViewItem.Checked = true;

                this.listView1.Items.Add(listViewItem);
            }
            GetExt();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            GetExt();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (carid == string.Empty)
            {
                MessageBox.Show("尚未选择要送的车！");
            }
            else
            {
                if (verifyCode.CheckCode(this.txtcode.Text.Trim()))
                {
                    code = this.txtcode.Text.Trim();
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                    MessageBox.Show("验证码错误！");
            }
        }

        private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            try
            {
                for (int i = 0; i < this.listView1.Items.Count; i++)
                {
                    ListViewItem listViewItem = this.listView1.Items[i];
                    if (i != e.Index)
                        listViewItem.Checked = false;
                    else
                        carid = listViewItem.SubItems[0].Text;
                    //if (listViewItem != e.Item)
                    //    listViewItem.Checked = false;
                    //else
                    //    listViewItem.Checked = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
